package cate.game.activity.specall;

import cate.common.table.activity.specall.row.SpeCallWarOrderBaseRow;
import cate.common.table.activity.specall.row.SpeCallWarOrderRewardRow;
import cate.common.table.d.GDFunc;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.specall.msg.SpeCallWarOrderDataResp;
import cate.game.activity.specall.msg.SpeCallWarOrderNumResp;
import cate.game.res.MixRes;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass("独立卡池战令数据")
public class SpeCallWarOrder extends SubActivitySingle<SpeCallActivity> {
    @NoteField(value = "普通奖励领取状态")
    public Map<Integer, Boolean> normalMap;

    @NoteField(value = "额外奖励领取状态")
    public Map<Integer, Boolean> extraMap;

    @NoteField("是否激活")
    public boolean active;

    @Override
    public void initialize(SpeCallActivity parent) {
        super.initialize(parent);
        if (normalMap == null) {
            normalMap = new HashMap<>();
        }
        if (extraMap == null) {
            extraMap = new HashMap<>();
        }
    }

    public int getNum(){
        return getStatus().getTotalNum();
    }

    @JsonIgnore
    public SpeCallSPO getStatus() {
        SpeCallHandler h = role.getGame().activityGlobal.getEnableHandler(GDFunc.SPE_CALL);
        return h.getGlobalData();
    }



    public void clear() {
        active = false;
        normalMap.clear();
        extraMap.clear();
    }

    public void noticeUpdate() {
        role.sendNow(new SpeCallWarOrderDataResp(this));
    }

    public void noticeNum(int num){
        role.sendNow(new SpeCallWarOrderNumResp(num));
    }

    private SpeCallWarOrderBaseRow configRow() {
        return role.getGame().table.speCall.warOrderBase.getRow(parent.configTid());
    }

    private SpeCallWarOrderRewardRow rewardRow(int id) {
        return role.getGame().table.speCall.warOrderReward.get(id);
    }

    public void addNum(int heroTid, int num) {
        SpeCallWarOrderBaseRow baseRow = configRow();
        if (baseRow == null || baseRow.heroTid == 0 || baseRow.heroTid != heroTid) {
            return;
        }
        noticeNum(getStatus().addTrueNum(num));
    }

    public EcResult<Void> onGetReward(int tid) {
        EcResult<Void> r = new EcResult<>();
        SpeCallWarOrderRewardRow row = rewardRow(tid);
        if (row == null) {
            return r.fail("配置错误");
        }
        if (getNum() < row.limit) {
            return r.fail("达成人数不足");
        }
        MixRes reward = new MixRes();
        if (!normalMap.containsKey(tid)) {
            reward.addList(new MixRes(row.baseRewardStr));
            normalMap.put(tid, true);
        }
        if (!extraMap.containsKey(tid) && active) {
            reward.addList(new MixRes(row.extraRewardStr));
            extraMap.put(tid, true);
        }
        if (reward.isEmpty()) {
            return r.fail("没有可以领取的奖励");
        }
        reward.add(role, true);
        role.getGame().notice.dialogReward(role, reward);
        noticeUpdate();
        return r.success();
    }

    public GameResult<Void> allowPay(PayItemRow payItemRow) {
        GameResult<Void> r = new GameResult<>();
        if (active) {
            return r.fail("不可重复激活");
        }
        return r;
    }

    public void onPaySuccess(PayItemRow payItemRow) {
        active = true;
        role.getHistory().action.speCallWarOrderActive();
        noticeUpdate();
    }
}
